package com.scuec.groupfive.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scuec.groupfive.entity.Questions;
import com.scuec.groupfive.mapper.QuestionMapper_z;
import com.scuec.groupfive.service.QuestionService_z;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuestionServiceImpl_z implements QuestionService_z {

    @Resource
    private QuestionMapper_z questionMapperZ;

    @Override
    public PageInfo<Questions> getQuestionList(Integer pageNum, Integer pageSize, String keyword) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Questions> questions = questionMapperZ.selectQuestions(keyword);
        return new PageInfo<>(questions);
    }

    @Override
    public Questions getQuestionById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("题目ID不能为空");
        }
        Questions question = questionMapperZ.selectById(id);
        if (question == null) {
            throw new RuntimeException("题目不存在");
        }
        return question;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveOrUpdateQuestion(Questions question) {
        if (question == null) {
            throw new IllegalArgumentException("题目信息不能为空");
        }
        if (!StringUtils.hasText(question.getContent())) {
            throw new IllegalArgumentException("题目内容不能为空");
        }
        if (question.getQuestionType() == null) {
            throw new IllegalArgumentException("题目类型不能为空");
        }

        int result = 0;
        if (question.getQuestionId() != null) {
            // 更新题目
            result = questionMapperZ.updateQuestion(question);

            // 根据题目类型更新对应的答案和解析
            switch (question.getQuestionType()) {
                case "single_choice":
                case "multiple_choice":
                    result += questionMapperZ.updateChoiceQuestion(question);
                    break;
                case "fill_in_the_blank":
                    result += questionMapperZ.updateFillBlankQuestion(question);
                    break;
                case "subjective":
                    result += questionMapperZ.updateSubjectiveQuestion(question);
                    break;
            }
        } else {
            // 新增题目
            result = questionMapperZ.insertQuestion(question);

            // 根据题目类型插入对应的答案和解析
            switch (question.getQuestionType()) {
                case "single_choice":
                case "multiple_choice":
                    result += questionMapperZ.insertChoiceQuestion(question);
                    break;
                case "fill_in_the_blank":
                    result += questionMapperZ.insertFillBlankQuestion(question);
                    break;
                case "subjective":
                    result += questionMapperZ.insertSubjectiveQuestion(question);
                    break;
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQuestion(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("题目ID不能为空");
        }

        // 先删除答题记录
        questionMapperZ.deleteAnswerRecords(id);

        // 删除试卷中的题目记录
        questionMapperZ.deleteExamQuestions(id);

        // 再删除关联表数据
        questionMapperZ.deleteChoiceQuestion(id);
        questionMapperZ.deleteFillBlankQuestion(id);
        questionMapperZ.deleteSubjectiveQuestion(id);

        // 最后删除主表数据
        return questionMapperZ.deleteQuestion(id);
    }

    @Override
    public List<Questions> getAllQuestions() {
        return questionMapperZ.selectAllQuestions();
    }
}